﻿#include "function_1d_measure.h"

Measure1D::Measure1D(QDialog* parent, QSettings* qsettings, QString section) : QDialog(parent)
{
    m_qsettings = qsettings;
    m_section = section;

    //m_model_image_path = QCoreApplication::applicationDirPath() + "/" + m_model_image_path;

    this->setWindowTitle(("宽度测量"));
    QRect desktop_rect = QApplication::desktop()->geometry();
    double ratio_x = desktop_rect.width()/1920.0;
    double ratio_y = desktop_rect.height()/1080.0;

    move(0,0);
    setFixedSize(1920*ratio_x,1010*ratio_y);

    m_view = new MyQGraphicsView(this);
    m_view->setGeometry(660 * ratio_x, 0, 1000 * 1.25 * ratio_x, 800 * 1.25 * ratio_y);

//    Row1=10;Column1=10;Row2=1000;Column2=1000;
//    m_view->setLinePose(10,10,Row2,Column2,Range);

    QSize control_size(150*ratio_x,40*ratio_y);

    QWidget* p = new QWidget(this);
    p->move(0*ratio_x,0*ratio_y);
    p->setFixedSize(600*ratio_x,800*ratio_y);

    QGridLayout *qgridLayout = new QGridLayout();
    p->setLayout(qgridLayout);

    int index = 0;

    QSize ButtonSize(160 * ratio_x, 40 * ratio_y);

  

//    m_qlabel_row1 = new QLabel();
//    m_qlabel_row1->setText("起始点y坐标");
//    m_qlabel_row1->setFixedSize(control_size);
//    qgridLayout->addWidget(m_qlabel_row1, index, 0);
//    m_qspinbox_row1 = new QSpinBox();
//    m_qspinbox_row1->setFixedSize(control_size);
//    m_qspinbox_row1->setMinimum(0);
//    m_qspinbox_row1->setMaximum(10000);
//    qgridLayout->addWidget(m_qspinbox_row1, index, 1);
    

    
//    index++;
//    m_qlabel_column1 = new QLabel();
//    m_qlabel_column1->setText("起始点x坐标");
//    m_qlabel_column1->setFixedSize(control_size);
//    qgridLayout->addWidget(m_qlabel_column1,index,0);
//    m_qspinbox_column1 = new QSpinBox();
//    m_qspinbox_column1->setFixedSize(control_size);
//    m_qspinbox_column1->setMinimum(0);
//    m_qspinbox_column1->setMaximum(10000);
//    qgridLayout->addWidget(m_qspinbox_column1, index, 1);

//    index++;
//    m_qlabel_row2 = new QLabel();
//    m_qlabel_row2->setText("结束点y坐标");
//    m_qlabel_row2->setFixedSize(control_size);
//    qgridLayout->addWidget(m_qlabel_row2, index, 0);
//    m_qspinbox_row2 = new QSpinBox();
//    m_qspinbox_row2->setFixedSize(control_size);
//    m_qspinbox_row2->setMinimum(0);
//    m_qspinbox_row2->setMaximum(10000);
//    qgridLayout->addWidget(m_qspinbox_row2, index, 1);
//    index++;

//    m_qlabel_column2_offset = new QLabel();
//    m_qlabel_column2_offset->setText("结束点x坐标");
//    m_qlabel_column2_offset->setFixedSize(control_size);
//    qgridLayout->addWidget(m_qlabel_column2_offset,index,0);
//    m_qspinbox_column2_offset = new QSpinBox();
//    m_qspinbox_column2_offset->setFixedSize(control_size);
//    m_qspinbox_column2_offset->setMinimum(0);
//    m_qspinbox_column2_offset->setMaximum(10000);
//    qgridLayout->addWidget(m_qspinbox_column2_offset, index, 1);

//    index++;
//    m_qlabel_roi_width = new QLabel();
//    m_qlabel_roi_width->setText("测量");
//    m_qlabel_roi_width->setFixedSize(control_size);
//    qgridLayout->addWidget(m_qlabel_roi_width, index, 0);
//    m_qspinbox_roi_width = new QSpinBox();
//    m_qspinbox_roi_width->setFixedSize(control_size);
//    m_qspinbox_roi_width->setMinimum(0);
//    m_qspinbox_roi_width->setMaximum(1000);
//    qgridLayout->addWidget(m_qspinbox_roi_width, index, 1);
    index++;
    m_qcombox_transition = new QComboBox();
    m_qcombox_transition->setFixedSize(control_size);
    m_qcombox_transition->addItem("从暗到明");
    m_qcombox_transition->addItem("从明到暗");
    qgridLayout->addWidget(m_qcombox_transition, index, 0);

    index++;
    m_qlabel_number = new QLabel();
    m_qlabel_number->setText("测量次数");
    m_qlabel_number->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_number,index,0);
    m_qspinbox_number = new QSpinBox();
    m_qspinbox_number->setFixedSize(control_size);
    m_qspinbox_number->setMinimum(3);
    m_qspinbox_number->setMaximum(1000);
    qgridLayout->addWidget(m_qspinbox_number, index, 1);

    index++;
    m_qlabel_range = new QLabel();
    m_qlabel_range->setText("测量范围");
    m_qlabel_range->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_range, index, 0);
    m_qspinbox_range = new QSpinBox();
    m_qspinbox_range->setFixedSize(control_size);
    m_qspinbox_range->setMinimum(10);
    m_qspinbox_range->setMaximum(10000);
    qgridLayout->addWidget(m_qspinbox_range, index, 1);

    index++;
    m_qlabel_threshold = new QLabel();
    m_qlabel_threshold->setText("测量梯度阈值");
    m_qlabel_threshold->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_threshold, index, 0);
    m_qspinbox_threshold = new QSpinBox();
    m_qspinbox_threshold->setFixedSize(control_size);
    m_qspinbox_threshold->setMinimum(10);
    m_qspinbox_threshold->setMaximum(255);
    qgridLayout->addWidget(m_qspinbox_threshold, index, 1);

    index++;
    m_qlabel_standard_short = new QLabel();
    m_qlabel_standard_short->setText("宽度最小标准");
    m_qlabel_standard_short->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_standard_short, index, 0);
    m_qspinbox_standard_short = new QSpinBox();
    m_qspinbox_standard_short->setFixedSize(control_size);
    m_qspinbox_standard_short->setMinimum(1);
    m_qspinbox_standard_short->setMaximum(10000);
    qgridLayout->addWidget(m_qspinbox_standard_short, index, 1);

    index++;
    m_qlabel_standard_long = new QLabel();
    m_qlabel_standard_long->setText("宽度最大标准");
    m_qlabel_standard_long->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_standard_long, index, 0);
    m_qspinbox_standard_long = new QSpinBox();
    m_qspinbox_standard_long->setFixedSize(control_size);
    m_qspinbox_standard_long->setMinimum(1);
    m_qspinbox_standard_long->setMaximum(10000);
    qgridLayout->addWidget(m_qspinbox_standard_long, index, 1);

    index++;
    m_pushbutton_save_parameter = new QPushButton(this);
    m_pushbutton_save_parameter->setFixedSize(control_size);
    m_pushbutton_save_parameter->setText("保存参数");
    connect(m_pushbutton_save_parameter, &QPushButton::clicked, this, &Measure1D::saveParameter);
    qgridLayout->addWidget(m_pushbutton_save_parameter, index,0);

    index++;
    m_pushbutton_detection = new QPushButton(this);
    m_pushbutton_detection->setFixedSize(control_size);
    m_pushbutton_detection->setText("检测");
    connect(m_pushbutton_detection, &QPushButton::clicked, this, &Measure1D::detectionImage);
    qgridLayout->addWidget(m_pushbutton_detection, index,0);
    
    index++;
    m_pushbutton_return = new QPushButton(this);
    m_pushbutton_return->setFixedSize(control_size);
    m_pushbutton_return->setText("退出");
    qgridLayout->addWidget(m_pushbutton_return, index, 0);
    connect(m_pushbutton_return, &QPushButton::clicked, this, [=]() {
        this->hide();
        });

    
    try
    {
        m_proc_detection = new HDevProcedure("measureEdgeWidths");
        m_call_detection = new HDevProcedureCall(*m_proc_detection);
    }
    catch (HDevEngineException& hdev_exception)
    {
        QMessageBox::information(nullptr, "", hdev_exception.Message());
    }


    loadParameter();
}

Measure1D::~Measure1D()
{
    ;
}

bool Measure1D::detection( HObject image_in, QString& message)
{
  /*  if (m_image.empty())
    {
        message += "检测图像为空 ";
        return false;
    }*/
    try
    {
        // Set the input parameters of the procedure
//        if (image_in.empty())
//        {
//            message += "检测图像为空 ";
//            return false;
//        }
        m_call_detection->SetInputIconicParamObject("Image", image_in);

        m_call_detection->SetInputCtrlParamTuple("Row1", Row1);
        m_call_detection->SetInputCtrlParamTuple("Column1", Column1);
        m_call_detection->SetInputCtrlParamTuple("Row2", Row2);
        m_call_detection->SetInputCtrlParamTuple("Column2", Column2);
        m_call_detection->SetInputCtrlParamTuple("RoiWidth", 3);
        m_call_detection->SetInputCtrlParamTuple("Threshold", Threshold);


        m_call_detection->SetInputCtrlParamTuple("Transition", Transition);
        m_call_detection->SetInputCtrlParamTuple("Number", Number);
        m_call_detection->SetInputCtrlParamTuple("Range", Range);

        // Execute the procedure

//        HDevEngine().StartDebugServer();
//        m_call_detection->SetWaitForDebugConnection(true);
        m_call_detection->Execute();
//        HDevEngine().StopDebugServer();



        // get the output parameters
        HTuple ret = m_call_detection->GetOutputCtrlParamTuple(1);
        RowEdgeFirstShort= m_call_detection->GetOutputCtrlParamTuple(2);
        ColumnEdgeFirstShort= m_call_detection->GetOutputCtrlParamTuple(3);
        RowEdgeSecondShort = m_call_detection->GetOutputCtrlParamTuple(4);
        ColumnEdgeSecondShort = m_call_detection->GetOutputCtrlParamTuple(5);
        DistanceShort = m_call_detection->GetOutputCtrlParamTuple(6);
        RowEdgeFirstLong = m_call_detection->GetOutputCtrlParamTuple(7);
        ColumnEdgeFirstLong = m_call_detection->GetOutputCtrlParamTuple(8);
        RowEdgeSecondLong = m_call_detection->GetOutputCtrlParamTuple(9);
        ColumnEdgeSecondLong = m_call_detection->GetOutputCtrlParamTuple(10);
        DistanceLong = m_call_detection->GetOutputCtrlParamTuple(11);

        m_result = ret[0].I();
        if (m_result == 1)
        {
            m_y1_short =RowEdgeFirstShort[0].D();
            m_x1_short =ColumnEdgeFirstShort[0].D();
            m_y2_short =RowEdgeSecondShort[0].D();
            m_x2_short =ColumnEdgeSecondShort[0].D();
            m_distance_short =DistanceShort[0].D();
            m_y1_long =RowEdgeFirstLong[0].D();
            m_x1_long =ColumnEdgeFirstLong[0].D();
            m_y2_long =RowEdgeSecondLong[0].D();
            m_x2_long =ColumnEdgeSecondLong[0].D();
            m_distance_long =DistanceLong[0].D();
            if(m_distance_short<m_standard_short)
            {
                message += "超过宽度最小标准";
                return false;
            }
            if(m_distance_long>m_standard_long)
            {
                message += "超过宽度最大标准";
                return false;
            }
            return true;
        }
        else
        {
            return false;
        }
    }
    catch (HDevEngineException& hdev_exception)
    {
        message = hdev_exception.Message();
        return false;
    }
  
    return true;
}

void Measure1D::detectionImage()
{
    if(m_image.empty())
    {
        QMessageBox::information(nullptr,"","没有图像");
        return;
    }

    QString message;
    HObject image = matToHImage(m_image);
    bool state = detection(image, message);

    Mat image_show = m_image.clone();
    if(image_show.empty())
    {
        QMessageBox::information(nullptr,"","没有图像");
        return;
    }
    draw(image_show, image_show);

    QImage q_image = matToQImage(image_show);

    m_view->setImage(q_image, message);
    m_view->setLinePose(Row1,Column1,Row2,Column2,Range);
    }

void Measure1D::draw(Mat image_in,Mat& image_out)
{
    if(image_in.empty())
        return;
    cv::line(image_out,cv::Point(m_x1_short,m_y1_short),cv::Point(m_x2_short,m_y2_short),Scalar(0, 255, 0), 3);
    cv::line(image_out,cv::Point(m_x1_long,m_y1_long),cv::Point(m_x2_long,m_y2_long),Scalar(0, 255, 0), 3);
    putText(image_out,"min:" + std::to_string(m_distance_short) ,cv::Point(m_x1_short,m_y1_short),FONT_HERSHEY_SIMPLEX,5,Scalar(0,255,0),3);
    putText(image_out,"max" + std::to_string(m_distance_long) ,cv::Point(m_x1_long,m_y1_long),FONT_HERSHEY_SIMPLEX,5,Scalar(0,255,0),3);
}

void Measure1D::loadParameter()
{

    Row1 = m_qsettings->value(m_section + "/" + "Row1",10).toInt();
    Column1 = m_qsettings->value(m_section + "/" + "Column1",10).toInt();
    Row2 = m_qsettings->value(m_section + "/" + "Row2",5000).toInt();
    Column2 = m_qsettings->value(m_section + "/" + "Column2",500).toInt();

    Threshold = m_qsettings->value(m_section + "/" + "Threshold").toInt();
    Number = m_qsettings->value(m_section + "/" + "Number").toInt();
    Range = m_qsettings->value(m_section + "/" + "Range").toInt();
    Transition = m_qsettings->value(m_section + "/" + "Transition").toInt();

    m_qspinbox_threshold->setValue(Threshold);
    m_qspinbox_number->setValue(Number);
    m_qspinbox_range->setValue(Range);
    m_qcombox_transition->setCurrentIndex(Transition);

    m_standard_short = m_qsettings->value(m_section + "/" + "m_standard_short").toInt();
    m_standard_long = m_qsettings->value(m_section + "/" + "m_standard_long").toInt();
    m_qspinbox_standard_short->setValue(m_standard_short);
    m_qspinbox_standard_long->setValue(m_standard_long);
//    m_qspinbox_row1->setValue(row1_offset);
//    m_qspinbox_column1->setValue(column1_offset);
//    m_qspinbox_row2->setValue(row2_offset);
//    m_qspinbox_column2_offset->setValue(column2_offset);
    return ;
}

void Measure1D::saveParameter()
{
    Row1 = m_view->m_graphics_shape_line->pos().y() + (int)m_view->m_graphics_shape_line->m_point1->y();
    Column1 = m_view->m_graphics_shape_line->pos().x() + (int)m_view->m_graphics_shape_line->m_point1->x();
    Row2 =  m_view->m_graphics_shape_line->pos().y() + (int)m_view->m_graphics_shape_line->m_point2->y();
    Column2 =  m_view->m_graphics_shape_line->pos().x() + (int)m_view->m_graphics_shape_line->m_point2->x();

    m_qsettings->setValue(m_section + "/" + "Row1", Row1);
    m_qsettings->setValue(m_section + "/" + "Column1", Column1);
    m_qsettings->setValue(m_section + "/" + "Row2", Row2);
    m_qsettings->setValue(m_section + "/" + "Column2", Column2);

    Threshold = m_qspinbox_threshold->value();
    Number = m_qspinbox_number->value();
    Range = m_qspinbox_range->value();
    Transition= m_qcombox_transition->currentIndex();
    m_standard_short = m_qspinbox_standard_short->value();
    m_standard_long  = m_qspinbox_standard_long->value();

    m_qsettings->setValue(m_section + "/" + "Threshold", Threshold);
    m_qsettings->setValue(m_section + "/" + "Number", Number);
    m_qsettings->setValue(m_section + "/" + "Range", Range);
    m_qsettings->setValue(m_section + "/" + "Transition", Transition);
    m_qsettings->setValue(m_section + "/" + "m_standard_short", m_standard_short);
    m_qsettings->setValue(m_section + "/" + "m_standard_long", m_standard_long);

    m_view->setLinePose(Row1,Column1,Row2,Column2,Range);
    return;
}

bool Measure1D::showDialog()
{
    QImage q_image = matToQImage(m_image);
    m_view->setImage(q_image, "");
    m_view->setLinePose(Row1,Column1,Row2,Column2,Range);
    return true;
}


